import re
import sqlite3
import requests


def get_url(key, index):
    url = 'http://search.dangdang.com/?key=%s&category_id=4003728&page_index=%s' % (key, index)
    return url


def get_data(url):
    try:
        data = requests.get(url)
        return data.text
    except:
        print('获取数据失败')


def deal_data(data):
    try:
        data = re.findall('''<li ddt-pit=.*?title="(.*?)".*?href="(http.*?)".*?img data-original='(.*?)'.*?price_n.*?;(.*?)<.*?</li>''',
                          data)
        return data
    except:
        print('异常')



def write_database(data,good_category):
    conn = sqlite3.connect("data.sqlite")
    cur = conn.cursor()
    for i in range(0, len(data)):
        name = data[i][0]
        link = data[i][1]
        img = data[i][2]
        price = float(data[i][3])
        category = good_category
        sql = '''insert into goods_info(goods_name,goods_link,goods_img,goods_category,goods_price)  values('%s', '%s','%s',%s,'%s')'''%(name,link,img,category,price)
        try:
            cur.execute(sql)
        except Exception as e:
            print(e)
    conn.commit()
    cur.close()
    conn.close()


def main():
    goods_category = {'有氧健身':1,'力量训练':2,'搏击训练':3,'懒人健身':4,'瑜伽训练':5}
    page = 10
    for key in goods_category.keys():
        print(key)
        good_category = goods_category[key]
        for i in range(1, page):
            url = get_url(key, i)
            data = get_data(url)
            data = deal_data(data)
            write_database(data,good_category)


if __name__ == '__main__':
    main()
    print('done')
